package org.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.qf.mapper.BillMapper;

import org.qf.mapper.GoodMapper;
import org.qf.mapper.ProviderMapper;
import org.qf.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BillService {
    @Resource
    public BillMapper billMapper;

    @Resource
    public GoodMapper goodMapper;
    @Resource
    public ProviderMapper providerMapper;

    public PageInfo<Bill> showUser(int pageNum, String goodName, String providerName, String isPay) {
        //当前页 页面容量
        List<Provider> providers = providerMapper.selectAll();
        List<Good> goods = goodMapper.selectAll();
        PageHelper.startPage(pageNum,3);
        Example example=new Example(Bill.class);
        Example.Criteria criteria=example.createCriteria();
        boolean flag=true;
        boolean flag1=true;

        for (Good good:goods) {
            if (good.getName().equals(goodName))
            {
                goodName=good.getGid();
                flag=false;
            }
        }


        for (Provider provider:providers) {
            if (provider.getProname().equals(providerName))
            {
                providerName=provider.getProid();
                flag=false;
            }
        }
        if (flag)
        {
            goodName=null;
        }
        if (flag1)
        {
            providerName=null;
        }
        if(!StringUtils.isEmpty(goodName)) {
            //通过用户名进行模糊查询
            criteria.andLike("goodid", '%' + goodName + '%');
        }
        if(!StringUtils.isEmpty(providerName)) {
            //通过用户名进行模糊查询
            criteria.andLike("providerid", '%' + providerName + '%');
        }
        if(!StringUtils.isEmpty(isPay)) {
            //通过用户名进行模糊查询
            criteria.andEqualTo("ispayment", '%' + isPay + '%');
        }
        List<Bill> bills =billMapper.selectByExample(example);
//        List<User> Users = userMapper.selectAll();

        //可以得到用户对象中的userRole
        //那么角色表中的id就可以赋值了
//        bindUser(Users);
        for (Bill bill: bills) {
            for (Good good:goods) {
                if (good.getGid().equals(bill.getGoodid()))
                {
                    bill.setGoodid(good.getName());
                }
            }
            for (Provider provider:providers) {
                if (provider.getProid().equals(bill.getProviderid()))
                {
                    bill.setProviderid(provider.getProname());
                }
            }

        }
        PageInfo<Bill> pageInfo=new PageInfo<Bill>(bills);
        return pageInfo;
    }

    public int addBill(Bill bill) {
        return billMapper.insert(bill);
    }

    public Bill selectByID(String billcode) {
        return billMapper.selectByPrimaryKey(billcode);
    }

    public int EditBill(Bill bill) {
        return billMapper.updateByPrimaryKeySelective(bill);
    }

    public int DeleteBill(Bill bill) {
        return billMapper.deleteByPrimaryKey(bill.getBillcode());
    }
}
